/**
 * 
 */
package ua.lk.phonebook;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import javax.naming.InitialContext;
import javax.sql.DataSource;
/**
 * @author lkonst
 * ����� � ��������� ������� � ���� ������
 */
public class Database {
//	private static final java.sql.Connection connection = null;
	static final String resource_name = "java:comp/env/jdbc/info09_2014";
	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	static final String DB_URL = "jdbc:mysql://localhost/info09_2014";	
	static final String USERNAME = "admin";
	static final String PASSWORD = "fuckoff";
	
	public static boolean usePool = true;
	
	
	public static Connection getConnection()
	{
		Connection conn = null;
		try {
			if( usePool )
			{
				InitialContext initContext = new InitialContext();
				DataSource ds = (DataSource) initContext.lookup(resource_name);
				conn = ds.getConnection();
			}
			else
			{
			    Class.forName(JDBC_DRIVER);
			    conn = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
			}
		}
		catch (Exception e){
			System.out.println("open connection error: "+e.getMessage());
		}
			
		return conn;
	}
	
	public static void close(Connection conn)
	{
		if(conn != null)
		try {
			conn.close();
		}
		catch(Exception e)
		{
			System.out.println("close connection error: "+e.getMessage());
		}
	}
	
	public static void close(Statement st)
	{
		if(st != null)
			try {
				st.close();
			}
			catch(Exception e)
			{
				System.out.println("close statement error: "+e.getMessage());
			}
	}
	
}
